package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.basic.WmsProduct;
import com.canaan.business.service.basic.IWmsProductService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.MaterialPreparationDetailMapper;
import com.canaan.business.domain.bill.MaterialPreparationDetail;
import com.canaan.business.service.bill.IMaterialPreparationDetailService;

/**
 * 生产备料清单Service业务层处理
 *
 * @author huangxs
 * @date 2023-09-13
 */
@Service
public class MaterialPreparationDetailServiceImpl implements IMaterialPreparationDetailService {
    @Resource
    private MaterialPreparationDetailMapper materialPreparationDetailMapper;
    @Resource
    private IWmsProductService wmsProductService;

    /**
     * 查询生产备料清单
     *
     * @param id 生产备料清单主键
     * @return 生产备料清单
     */
    @Override
    public MaterialPreparationDetail selectMaterialPreparationDetailById(Long id) {
        return materialPreparationDetailMapper.selectMaterialPreparationDetailById(id);
    }

    /**
     * 查询生产备料清单列表
     *
     * @param materialPreparationDetail 生产备料清单
     * @return 生产备料清单
     */
    @Override
    public List<MaterialPreparationDetail> selectMaterialPreparationDetailList(MaterialPreparationDetail materialPreparationDetail) {
        return materialPreparationDetailMapper.selectMaterialPreparationDetailList(materialPreparationDetail);
    }

    /**
     * 新增生产备料清单
     *
     * @param materialPreparationDetail 生产备料清单
     * @return 结果
     */
    @Override
    public int insertMaterialPreparationDetail(MaterialPreparationDetail materialPreparationDetail) {
        if(StringUtils.isBlank(materialPreparationDetail.getNo())){
            materialPreparationDetail.setNo(TableNoUtil.getNo(MaterialPreparationDetail.class));
        }
        WmsProduct product = wmsProductService.selectWmsProductByProductNo(materialPreparationDetail.getProductNo());
        if (product!=null){
            materialPreparationDetail.setCompany(product.getCompany());
        }else{
            throw new ServiceException("找不到物料");
        }
        materialPreparationDetail.setCreateBy(SecurityUtils.getUsername());
        materialPreparationDetail.setCreateTime(DateUtils.getNowDate());
        return materialPreparationDetailMapper.insertMaterialPreparationDetail(materialPreparationDetail);
    }

    /**
     * 批量新增生产备料清单
     *
     * @param materialPreparationDetailList 生产备料清单列表
     * @return 结果
     */
    @Override
    public int batchInsertMaterialPreparationDetail(List<MaterialPreparationDetail> materialPreparationDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (MaterialPreparationDetail materialPreparationDetail : materialPreparationDetailList){
            if(StringUtils.isBlank(materialPreparationDetail.getNo())){
                materialPreparationDetail.setNo(TableNoUtil.getNo(MaterialPreparationDetail.class));
            }
            WmsProduct product = wmsProductService.selectWmsProductByProductNo(materialPreparationDetail.getProductNo());
            if (product!=null){
                materialPreparationDetail.setCompany(product.getCompany());
            }
            materialPreparationDetail.setCreateBy(username);
            materialPreparationDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<materialPreparationDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > materialPreparationDetailList.size()) {
                endIndex = materialPreparationDetailList.size();
            }
            insertNum = insertNum + materialPreparationDetailMapper.batchInsertMaterialPreparationDetail(materialPreparationDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改生产备料清单
     *
     * @param materialPreparationDetail 生产备料清单
     * @return 结果
     */
    @Override
    public int updateMaterialPreparationDetail(MaterialPreparationDetail materialPreparationDetail) {
        WmsProduct product = wmsProductService.selectWmsProductByProductNo(materialPreparationDetail.getProductNo());
        if (product!=null){
            materialPreparationDetail.setCompany(product.getCompany());
        }else{
            throw new ServiceException("找不到物料");
        }
        materialPreparationDetail.setUpdateBy(SecurityUtils.getUsername());
        materialPreparationDetail.setUpdateTime(DateUtils.getNowDate());
        return materialPreparationDetailMapper.updateMaterialPreparationDetail(materialPreparationDetail);
    }

    /**
     * 批量修改生产备料清单
     *
     * @param materialPreparationDetailList 生产备料清单列表
     * @return 结果
     */
    @Override
    public int batchUpdateMaterialPreparationDetail(List<MaterialPreparationDetail> materialPreparationDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (MaterialPreparationDetail materialPreparationDetail : materialPreparationDetailList){
            WmsProduct product = wmsProductService.selectWmsProductByProductNo(materialPreparationDetail.getProductNo());
            if (product!=null){
                materialPreparationDetail.setCompany(product.getCompany());
            }
            materialPreparationDetail.setUpdateBy(username);
            materialPreparationDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<materialPreparationDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > materialPreparationDetailList.size()) {
                endIndex = materialPreparationDetailList.size();
            }
            updateNum = updateNum + materialPreparationDetailMapper.batchUpdateMaterialPreparationDetail(materialPreparationDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除生产备料清单
     *
     * @param ids 需要删除的生产备料清单主键集合
     * @return 结果
     */
    @Override
    public int deleteMaterialPreparationDetailByIds(Long[] ids) {
        return materialPreparationDetailMapper.deleteMaterialPreparationDetailByIds(ids);
    }

    /**
     * 删除生产备料清单信息
     *
     * @param id 生产备料清单主键
     * @return 结果
     */
    @Override
    public int deleteMaterialPreparationDetailById(Long id) {
        return materialPreparationDetailMapper.deleteMaterialPreparationDetailById(id);
    }

    /**
     * 查询生产备料清单
     *
     * @param no 编号
     * @return 生产备料清单
     */
    @Override
    public MaterialPreparationDetail selectMaterialPreparationDetailByNo(String no) {
        return materialPreparationDetailMapper.selectMaterialPreparationDetailByNo(no);
    }

    /**
     * 批量删除生产备料清单
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteMaterialPreparationDetailByNos(String[] nos) {
        return materialPreparationDetailMapper.deleteMaterialPreparationDetailByNos(nos);
    }

    /**
     * 删除生产备料清单信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteMaterialPreparationDetailByNo(String no) {
        return materialPreparationDetailMapper.deleteMaterialPreparationDetailByNo(no);
    }

    @Override
    public List<MaterialPreparationDetail> selectByOrderNo(String orderNo) {
        MaterialPreparationDetail queryParam = new MaterialPreparationDetail();
        queryParam.setOrderNo(orderNo);
        return this.materialPreparationDetailMapper.selectMaterialPreparationDetailList(queryParam);
    }
}
